﻿<?php
	class jrmail
	{
		private   $authkey = 333333;
		private	  $isauth = false;
		protected $header = '';
		protected $content = '';
		protected $mail_type = 'html';
		protected $sender = '';
		protected $sender_name = '';
		protected $receiver = '';
		protected $receiver_list = '';
		protected $receiver_list_delim = ',';
		protected $isAttachment = false;
		protected $subject = '';
		protected $send_list = false;
		protected $listfromurl = false;
		protected $params = '';
		public $numofsent = 0;
		private function printInfor($rx)
		{
			echo "Receiver: ".$rx."<br />";
			echo "Sender: ".$this->sender_name." ".$this->sender." <br />";
			echo "Subject: ".$this->subject."<br />";
			echo "Content: ".$this->content."<br />";
		}
		function __construct($delim = ',')
		{
			$this->receiver_list_delim = $delim;
		}
		function authentication($key)
		{
			if($this->authkey == $key)
				$this->isauth = true;
			return $this->isauth;
		}
		public function setAll($sx,$sx_name,$rx,$sub,$cont,$hder,$params,$is_list = false,$fromurl = false)
		{
			if(!$is_list)
				$this->setReceiver($rx);
			else
				$this->setReceiverList($rx,$fromurl);
			$this->setSender($sx,$sx_name);
			$this->setSubject($sub);
			$this->setMailContent($cont);
			$this->setCustomHeader($hder);
			$this->setAdditionParams($params);
		}
		function setMailContent($var)
		{
			$this->content = $var;
		}
		function setSubject($var)
		{
			$this->subject = $var;
		}
		function setSender($var1,$var2)
		{
			$this->sender = $var1;
			$this->sender_name = $var2;
		}
		function setReceiver($var)
		{
			$this->receiver = $var;
		}
		function setReceiverList($var,$var2=false)
		{
			$this->receiver_list = $var;
			$this->send_list = true;
			$this->listfromurl = $var2;
		}
		function setCustomHeader($var)
		{
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			$headers .= 'From: '. $this->sender_name. '<'.$this->sender.'>' . "\r\n";
			$this->header = $headers.$var;
		}
		function setAdditionParams($var)
		{
			$this->params = $var;
		}
		private function sendOne($rcver)
		{
			$result = false;
			if(!$this->send_list)
				$this->printInfor($rcver);
				
			if(version_compare(phpversion(), '4.2.3', '=') || (version_compare(phpversion(), '4.0.5', '<'))) //$this->params hadn't been supported
				$result = mail($rcver , $this->subject , $this->content, $this->header);
			else
				$result = mail($rcver , $this->subject , $this->content, $this->header,$this->params);
			if($result)
			{	
				$this->numofsent ++;
				echo "Success! ".$this->numofsent." message(s) was sent! <br />";
			}
		}
		private function sendList()
		{
			if(!$this->send_list)
				return;
			$rxlist = explode($this->receiver_list_delim,$this->receiver_list);
			for($i = 0; $i < count($rxlist); $i++)
			{
				$this->sendOne($rxlist[$i]);
			}
		}
		private function sendListUrl()
		{
			$f = fopen($this->receiver_list,'r');
			while(($buf = fgets($f)) !== false)
			{
				$this->sendOne($buf);
			}
			fclose($f);
		}
		function send()
		{
			if(!$this->isauth)
				return;
			if($this->send_list)
				if($this->listfromurl)
					$this->sendListUrl();
				else
					$this->sendList();
			else
				$this->sendOne($this->receiver);
		}
	}
?>
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
<?php
	$auth = 0000;
	$type = 1; //1: post, 0: get
	$islist = false;
	$rxdelim = ',';
	$fromurl = false;
	if(isset($_GET['authkey']))
	{
		$auth = $_GET['authkey'];
		$type = 0;
	}
	else if(isset($_POST['authkey']))
	{
		$auth = $_POST['authkey'];
		if($_POST['islist'] == "list")
		{
			$rxdelim = $_POST['rxdelim'];
			if($rxdelim == "")
				$rxdelim = ",";
			if($_POST['fromurl'] == 1)
				$fromurl = true;
			$islist = true;
		}
		$type = 1;
	}
	else
	{
		return;
	}
	$mhdl = new jrmail($rxdelim);
	if(!$mhdl->authentication($auth))
		return;
	if($type == 1) //Only support
	{
		$subject = $_POST['subject'];
		$rx = $_POST['rx'];
		//echo $rx;
		$content = $_POST['content'];
		$sx = 'admin@cungmua.com';
		$sxname = 'Cung Mua';
		if(isset($_POST['sx']))
		{
			$sx = $_POST['sx'];
			$sxname = $_POST['sx_name'];
		}
		$mhdl->setAll($sx,$sxname,$rx,$subject,$content,'','',$islist,$fromurl);
		$mhdl->send();
	}
	else
	{
		$subject = $_GET['subject'];
		$rx = $_GET['rx'];
		$content = $_GET['content'];
		$sx = 'admin@cungmua.com';
		$sxname = 'Cung Mua';
		if(isset($_GET['sx']))
		{
			$sx = $_GET['sx'];
			$sxname = $_GET['sx_name'];
		}
		$mhdl->setAll($sx,$sxname,$rx,$subject,$content,'','',$islist);
		$mhdl->send();
	}
?>